package com.project.card;

import static com.project.card.util.UrlUtil.SERVER_URL;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OrderActivity extends AppCompatActivity {
    private TextView textTitle;
    private EditText editAddr;
    private Button button;
    private Intent intent;
    private SharedPreferences sp;
    private boolean flag=false;

    public static final ObjectMapper JSONMAPPER = new ObjectMapper()
            .enable(JsonParser.Feature.IGNORE_UNDEFINED)
            .enable(JsonParser.Feature.ALLOW_COMMENTS)
            .enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES)
            .enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES)
            .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        
        textTitle=findViewById(R.id.order_text_title);
        editAddr=findViewById(R.id.order_edit_addr);
        button=findViewById(R.id.order_confirm);
        OkHttpClient client =new OkHttpClient();
        intent=getIntent();
        String title = intent.getStringExtra("title");
        Integer gid = intent.getIntExtra("id",0);
        Integer price = intent.getIntExtra("price",0);
        textTitle.setText(title);
        sp=getSharedPreferences("current_user",MODE_PRIVATE);
        Integer uid = sp.getInt("id",0);
        String phone = sp.getString("phone","");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            FormBody.Builder params = new FormBody.Builder();
                            params.add("phone",phone);
                            Request request = new Request.Builder()
                                    .url(SERVER_URL+"user/findp")
                                    .post(params.build())
                                    .build();
                            Response response = client.newCall(request).execute();
                            String responseData = response.body().string();
                            JsonNode userNode = JSONMAPPER.readTree(responseData);
                            Integer balance = userNode.get("data").get("balance").asInt();
                            if(balance < price){
                                Toast.makeText(OrderActivity.this, "积分不足,请尝试获取积分", Toast.LENGTH_SHORT).show();
                            }else{
                                flag=true;
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        try{
                            if(flag){
                                FormBody.Builder params = new FormBody.Builder();
                                params.add("uid",String.valueOf(uid));
                                params.add("gid",String.valueOf(gid));
                                params.add("addr",editAddr.getText().toString());
                                Request request = new Request.Builder()
                                        .url(SERVER_URL+"order/order")
                                        .post(params.build())
                                        .build();
                                Response response = client.newCall(request).execute();
                                String responseData = response.body().string();
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
                finish();
            }
        });
        
        
    }
}